﻿using System;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// This is a controller class for renaming all aspects of a project to use standard naming conventions.
    /// I.e. It will rename the database table to be equal to the ClassName, the PKProp to be ClassName + ID.
    /// The FKProp to be RelationshipNameID etc.
    /// The DBFieldName to be PropName
    /// </summary>
    public class SolutionRenamer
    {
        public DMSolution Solution { get; private set; }

        public SolutionRenamer(DMSolution solution)
        {
            if (solution == null) throw new ArgumentNullException("solution");
            Solution = solution;
        }

        public void RenameAllClassesToStandardNames()
        {
            this.Solution.AllClasses.ForEach(dmClass => dmClass.RenameClass());
        }

        /// <summary>
        /// Renames the <see cref="Solution"/>s<see cref="DMSolution.DefaultAssembly"/> with the new solution name.
        /// If the solution has a default assembly with a standard name i.e. with the 
        /// previous solution name then it is renamed to a new standard assembly name.
        /// (Typically the standard assembly name is solutionName.BO.)
        /// 
        /// Uses the Solution.SolutionName Persisted as the initialSolutionName and the 
        /// Solution.Solution name as the new solution name.
        /// </summary>
        /// <param name="newSolutionName"></param>
        public void RenameSolution(string newSolutionName)
        {
            this.Solution.SolutionName = newSolutionName;
            if (Solution.DefaultAssembly == null) return;

            this.Solution.DefaultAssembly.AssemblyName = newSolutionName + DefaultAssemblyBOSuffix;
        }

        public static string DefaultAssemblyBOSuffix { get { return ".BO"; } }
    }
}